package ruifeng.zcj.dig.base.api;

import android.content.Context;
import android.util.Log;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

public class ApiHttpClient {

    public static String HOST = "www.oschina.net";
    public static String API_URL = "http://kw.runfkj.com/%s";
    //本地服务器地址
    public static String HOST_LOCAL = "http://192.168.1.101:8080/";
    public static String API_URL_LOCAL = HOST_LOCAL + "%s";

    // public final static String HOST = "192.168.1.46";
    // private static String API_URL = "http://192.168.1.46/%s";
    public static final String DELETE = "DELETE";
    public static final String GET = "GET";
    public static final String POST = "POST";
    public static final String PUT = "PUT";
    public static HttpUtils client;

    public ApiHttpClient() {

    }

    public static HttpUtils getHttpClient() {
        return client;
    }

    public static void cancelAll(Context context) {
//        client.cancelRequests(context, true);
    }

    public static String getAbsoluteApiUrl(String partUrl) {
        String url = String.format(API_URL, partUrl);
        Log.d("BASE_CLIENT", "request:" + url);
        System.out.println("BASE_CLIENT"+url);
        return url;
    }

    public static String getApiUrl() {
        return API_URL;
    }


    public static void log(String log) {
        Log.d("BaseApi", log);

    }

    public static void post(String partUrl, RequestCallBack<String> handler) {
//        client.post(getAbsoluteApiUrl(partUrl), handler);
        client.send(HttpRequest.HttpMethod.POST, getAbsoluteApiUrl(partUrl), handler);
        // log(new StringBuilder("POST ").append(partUrl).toString());
    }

    public static void post(String partUrl, RequestParams params,
                            RequestCallBack<String> handler) {
        client.send(HttpRequest.HttpMethod.POST, getAbsoluteApiUrl(partUrl), params, handler);
    }


    public static void setApiUrl(String apiUrl) {
        API_URL = apiUrl;
    }

    public static void setHttpClient(HttpUtils c) {
        client = c;
//        client.configSoTimeout(77777);

    }


    public static void setCookie(String cookie) {
//        client.addHeader("Cookie", cookie);

    }

    private static String appCookie;

    public static void cleanCookie() {
        appCookie = "";
    }

  /*  public static String getCookie(AppContext appContext) {
        if (appCookie == null || appCookie == "") {
            appCookie = appContext.getProperty("cookie");
        }
        return appCookie;
    }*/
}
